## No documentation for 'wineQualityReds' in specified packages and libraries:
## you could try '??wineQualityReds'
## 'data.frame': 1599 obs. of 13 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ fixed.acidity : num 7.4 7.8 7.8 11.2 7.4 7.4 7.9 7.3 7.8 7.5 ...
## $ volatile.acidity : num 0.7 0.88 0.76 0.28 0.7 0.66 0.6 0.65 0.58 0.5 ...
## $ citric.acid : num 0 0 0.04 0.56 0 0 0.06 0 0.02 0.36 ...
## $ residual.sugar : num 1.9 2.6 2.3 1.9 1.9 1.8 1.6 1.2 2 6.1 ...
## $ chlorides : num 0.076 0.098 0.092 0.075 0.076 0.075 0.069 0.065 0.073 0.071 ...
## $ free.sulfur.dioxide : num 11 25 15 17 11 13 15 15 9 17 ...
## $ total.sulfur.dioxide: num 34 67 54 60 34 40 59 21 18 102 ...
## $ density : num 0.998 0.997 0.997 0.998 0.998 ...
## $ pH : num 3.51 3.2 3.26 3.16 3.51 3.51 3.3 3.39 3.36 3.35 ...
## $ sulphates : num 0.56 0.68 0.65 0.58 0.56 0.56 0.46 0.47 0.57 0.8 ...
## $ alcohol : num 9.4 9.8 9.8 9.8 9.4 9.4 9.4 10 9.5 10.5 ...
## $ quality : int 5 5 5 6 5 5 5 7 7 5 ...
## X fixed.acidity volatile.acidity citric.acid
## Min. : 1.0 Min. : 4.60 Min. :0.1200 Min. :0.000
## 1st Qu.: 400.5 1st Qu.: 7.10 1st Qu.:0.3900 1st Qu.:0.090
## Median : 800.0 Median : 7.90 Median :0.5200 Median :0.260
## Mean : 800.0 Mean : 8.32 Mean :0.5278 Mean :0.271
## 3rd Qu.:1199.5 3rd Qu.: 9.20 3rd Qu.:0.6400 3rd Qu.:0.420
## Max. :1599.0 Max. :15.90 Max. :1.5800 Max. :1.000
## residual.sugar chlorides free.sulfur.dioxide
## Min. : 0.900 Min. :0.01200 Min. : 1.00
## 1st Qu.: 1.900 1st Qu.:0.07000 1st Qu.: 7.00
## Median : 2.200 Median :0.07900 Median :14.00
## Mean : 2.539 Mean :0.08747 Mean :15.87
## 3rd Qu.: 2.600 3rd Qu.:0.09000 3rd Qu.:21.00
## Max. :15.500 Max. :0.61100 Max. :72.00
## total.sulfur.dioxide density pH sulphates
## Min. : 6.00 Min. :0.9901 Min. :2.740 Min. :0.3300
## 1st Qu.: 22.00 1st Qu.:0.9956 1st Qu.:3.210 1st Qu.:0.5500
## Median : 38.00 Median :0.9968 Median :3.310 Median :0.6200
## Mean : 46.47 Mean :0.9967 Mean :3.311 Mean :0.6581
## 3rd Qu.: 62.00 3rd Qu.:0.9978 3rd Qu.:3.400 3rd Qu.:0.7300
## Max. :289.00 Max. :1.0037 Max. :4.010 Max. :2.0000
## alcohol quality
## Min. : 8.40 Min. :3.000
## 1st Qu.: 9.50 1st Qu.:5.000
## Median :10.20 Median :6.000
## Mean :10.42 Mean :5.636
## 3rd Qu.:11.10 3rd Qu.:6.000
## Max. :14.90 Max. :8.000
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.60 7.10 7.90 8.32 9.20 15.90
图形看起来有一些左偏,经过对数转换后近似于正态分布,峰值处于8.0左右。
图形显示出了两个峰值,分别在0.42和0.58附近
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.090 0.260 0.271 0.420 1.000
数值0的数量最多,远高于其他数值。
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
图像显示为长尾型,经过对数转换后接近正态分布,峰值出现在2.25附近。
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
图形为左偏长尾,经过对数转换后接近正态分布,峰值出现在0.08附近。
图形为左偏长尾,经过对数转换后接近双峰分布,峰值出现在6和16附近。
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
图形为左偏长尾,经过对数转换后接近正态分布,峰值出现在42附近。
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.9901 0.9956 0.9968 0.9967 0.9978 1.0037
图形接近正态分布,最小值0.9901,最大值1.0037,中位数0.9967,均值0.9978。
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.740 3.210 3.310 3.311 3.400 4.010
图形接近正态分布,最小值2.74,最大值4.01,中位数3.31,均值3.311
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
图形为左偏长尾,经过对数转换后接近正态分布,峰值出现在0.6附近。
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
图形为左偏,经过对数转换后依然左偏,峰值出现在9.5附近。
大部分的评分为5和6。
我的数据集选用了Udacity提供的“红葡萄酒质量”数据集。该数据集共有1599行(1599种红葡萄酒)、13列(分别表示11个关于红酒的化学成分变量、1项红酒质量评分和序号)。
我最感兴趣的特性是酒的质量评分,即quality。
通过阅读数据集的官方说明,我认为volatile.acidity,citric.acid,residual.sugar,total.sulfur.dioxide与quality关系比较密切,存在相关性。
没有创建新的变量。
volatile.acidity,citric.acid等特性存在异常值,我会在坐标轴上选取合适的范围来排除异常值的影响。
通过上图发现,quality与其他特性并没有很强的相关性,只与alcohol特性存在中度的正相关性。
theme_set(theme_minimal(8))
pf$quality = factor(pf$quality)
ggplot(aes(quality, volatile.acidity), data = pf) +
geom_jitter(alpha = .25) +
geom_boxplot(alpha = .25, color = 'blue') +
scale_y_continuous(breaks = seq(0, 1.6, .1)) +
stat_summary(fun.y = 'mean', geom = 'point', color = 'red')
不同评分的酒,均含有相同的volatile.acidity数值,没有体现出明显的相关性。在质量评分为5分的酒中,volatile.acidity的数值集中在0.45-0.65之间;在质量评分为6分的酒中,volatile.acidity的数值集中在0.4-0.6之间。
ggplot(aes(quality, citric.acid), data = pf) +
geom_point(alpha = .25, position = position_jitter(h=0)) +
geom_boxplot(alpha = .25, color = 'blue') +
stat_summary(fun.y = 'mean', geom = 'point', color = 'red') +
scale_y_continuous(breaks = seq(0, 1, .1))
不同评分的酒,均含有相同的citric.acid数值,没有体现出明显的相关性。在质量评分为5分的酒中,citric.acid的数值集中在0.09-0.35之间;在质量评分为6分的酒中,citric.acid的数值集中在0.08-0.43之间。
ggplot(aes(quality, residual.sugar), data = pf) +
geom_jitter(alpha = .1) +
geom_boxplot(alpha = .1, color = 'blue') +
stat_summary(fun.y = mean, geom = 'point', color = 'red') +
scale_y_continuous(breaks = seq(0, 16, 1))
summary(subset(pf, pf$quality==5)$residual.sugar)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.200 1.900 2.200 2.529 2.600 15.500
summary(subset(pf, pf$quality==6)$residual.sugar)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.900 1.900 2.200 2.477 2.500 15.400
不同评分的酒,均含有相同的residual.sugar数值,没有体现出明显的相关性。在质量评分为5分的酒中,residual.sugar的数值集中在1.9-2.6之间;在质量评分为6分的酒中,residual.sugar的数值集中在1.9-2.5之间。
pf$quality = as.numeric(levels(pf$quality))[pf$quality]
ggplot(aes(quality, residual.sugar), data = pf) +
coord_cartesian(ylim = c(quantile(pf$residual.sugar, .05),
quantile(pf$residual.sugar, .95))) +
scale_y_continuous(breaks = seq(quantile(pf$residual.sugar, .05),
quantile(pf$residual.sugar, .95),
.2)) +
geom_point(alpha = 0.1, color = 'orange') +
geom_line(stat = 'summary', fun.y = mean) +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(prob = .1),
linetype = 2, color = 'blue') +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(prob = .5),
color = 'blue') +
geom_line(stat = 'summary', fun.y = quantile, fun.args = list(prob = .9),
linetype = 2, color = 'blue')
不同质量的酒的residual.sugar数值,中位数的均值的变化比较平缓,但是第三四分位数的变化较大。可能是因为除了评分5和6的酒,其他分值的酒样本较少,第三四分位数受极值影响较大。
pf$quality = factor(pf$quality)
ggplot(aes(quality, total.sulfur.dioxide), data = pf) +
geom_jitter(alpha = .1) +
geom_boxplot(alpha = .1, color = 'blue') +
stat_summary(fun.y = 'mean', geom = 'point', color = 'red') +
coord_cartesian(ylim = c(0,170)) +
scale_y_continuous(breaks = seq(0, 200, 5))
library(reshape2)
summary(dcast(subset(pf, quality==5 | quality==6),
X~quality, value.var = 'total.sulfur.dioxide'))
## X 5 6
## Min. : 1.0 Min. : 6.00 Min. : 6.00
## 1st Qu.: 382.5 1st Qu.: 26.00 1st Qu.: 23.00
## Median : 768.0 Median : 47.00 Median : 35.00
## Mean : 793.0 Mean : 56.51 Mean : 40.87
## 3rd Qu.:1219.5 3rd Qu.: 84.00 3rd Qu.: 54.00
## Max. :1599.0 Max. :155.00 Max. :165.00
## NA's :638 NA's :681
不同评分的酒,均含有相同的total.sulfur.dioxide数值,没有体现出明显的相关性。在质量评分为5分的酒中,total.sulfur.dioxide的数值集中在26-84之间,均值和中位数分别为56.51和47;在质量评分为6分的酒中,total.sulfur.dioxide的数值集中在23-54之间,均值和中位数分别为40.87和35。
ggplot(aes(fixed.acidity, volatile.acidity), data = pf) +
geom_point(alpha = .4) +
geom_smooth(method = 'lm', color = 'orange')
with(pf, cor.test(fixed.acidity, volatile.acidity))
##
## Pearson's product-moment correlation
##
## data: fixed.acidity and volatile.acidity
## t = -10.589, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.3013681 -0.2097433
## sample estimates:
## cor
## -0.2561309
fixed.acidity和volatile.acidity之间存在较弱的负相关性
ggplot(aes(fixed.acidity, citric.acid), data = pf) +
geom_point(alpha = .4) +
geom_smooth(method = 'lm', color = 'orange')
with(pf, cor.test(fixed.acidity, citric.acid))
##
## Pearson's product-moment correlation
##
## data: fixed.acidity and citric.acid
## t = 36.234, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6438839 0.6977493
## sample estimates:
## cor
## 0.6717034
fixed.acidity和citric.acid之间存在较强的正相关性
ggplot(aes(volatile.acidity, citric.acid), data = pf) +
geom_point(alpha = .4) +
coord_cartesian(ylim = c(0,1)) +
geom_smooth(method = 'lm', color = 'orange')
with(pf, cor.test(volatile.acidity, citric.acid))
##
## Pearson's product-moment correlation
##
## data: volatile.acidity and citric.acid
## t = -26.489, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.5856550 -0.5174902
## sample estimates:
## cor
## -0.5524957
volatile.acidity和citric.acid之间存在较强的负相关性
ggplot(aes(free.sulfur.dioxide, total.sulfur.dioxide), data = pf) +
geom_point(alpha = .2)
with(pf, cor.test(free.sulfur.dioxide, total.sulfur.dioxide))
##
## Pearson's product-moment correlation
##
## data: free.sulfur.dioxide and total.sulfur.dioxide
## t = 35.84, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6395786 0.6939740
## sample estimates:
## cor
## 0.6676665
free.sulfur.dioxide和total.sulfur.dioxide之间存在较强的正相关性
ggplot(aes(fixed.acidity, pH), data = pf) +
geom_point(alpha = .2)
with(pf, cor.test(fixed.acidity, pH))
##
## Pearson's product-moment correlation
##
## data: fixed.acidity and pH
## t = -37.366, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.7082857 -0.6559174
## sample estimates:
## cor
## -0.6829782
fixed.acidity和pH之间存在较强的负相关性。
这些感兴趣的特性与数据集内其他特性与quality均没有较强的相关性。
fixed.acidity,volatile.acidity,citric.acid之间存在明显的相关性;free.sulfur.dioxide和total.sulfur.dioxide之间存在较强的正相关性;fixed.acidity和pH之间存在较强的负相关性。
最强的关系为fixed.acidity与pH,相关系数为-0.683。
可以看出,pH值随fixed.acidity值的增加呈下降趋势。
ggplot(aes(free.sulfur.dioxide, total.sulfur.dioxide), data = pf) +
geom_point(aes(color = quality))
可以看出,total.sulfur.dioxiede值随free.sulfur.dioxiede值的增加呈上升趋势。
ggplot(aes(volatile.acidity, citric.acid), data = pf) +
geom_point(aes(color = quality))
可以看出,citric.acid值随volatile.acidity值的增加呈下降趋势。
可以看出,pH值值随fixed.acidity值的增加而减少;total.sulfur.dioxiede值随free.sulfur.dioxiede值的增加而增加;citric.acid值随volatile.acidity值的增加而减少。
相同范畴的特性之间联系较大(比如fixed.acidity,volatile.acidity,citric.acid或者free.sulfur.dioxide,total.sulfur.dioxide)
可以看出,对于不同评分的酒,total.sulfur.dioxiede值均随free.sulfur.dioxiede值的增加呈上升趋势。
可以看出,对于不同评分的酒,citric.acid值均随volatile.acidity值的增加呈下降趋势。
可以看出,对于不同评分的酒,pH值均随fixed.acidity值的增加而降低。
我在画箱线图时,开始时无法自动根据quality的值进行分组,显示出来的是表示全部数据的箱线图。查看数据类型后,将quality原来的num属性改为factor属性,即可以进行自动分组。
但是factor属性的quality无法在绘图时画出汇总值(四分位置,均值等),我在将factor属性转换回num属性时遇到了麻烦,quality的数值发生了变化。通过查看文档,找到了将factor属性转换为num属性而不改变原始数值的方法。
开始时,我认为数据间会存在明显的相关性,但随着分析的深入,发现相关性并没有那么明显。例如,我在分析residual.sugar和pH的关系时,开始时认为residual.sugar的值越小,pH也应该越小,但通过分析发现,二者并没有相关性。
通过进一步分析发现,相同范畴的特性之间联系较大(比如fixed.acidity,volatile.acidity,citric.acid或者free.sulfur.dioxide,total.sulfur.dioxide)
在未来的分析中,应当注意不要在分析数据前就形成先入为主的想法,容易造成方向错误。在着手绘图前,应首先查看数据集的数值类型,及时转换数值类型。